package bd

import android.graphics.BitmapFactory
import android.os.Environment
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
import java.util.concurrent.CountDownLatch

class FF {
    companion object{
        private val TAG = "LocationSourceUtils=="
        private val IMAGE_TYPE = 0
        private val VIDEO_TYPE = 1
        private val imgList: ArrayList<FB> = ArrayList()
        private val videoList: ArrayList<FB> = ArrayList()
        private var countDownLatch: CountDownLatch? = null
        private var startTime = System.currentTimeMillis()
//        @OptIn(DelicateCoroutinesApi::class)
        fun getSourceFile(onLocationSourceListener: OnLocationSourceListener){
            imgList.clear()
            videoList.clear()
            startTime = System.currentTimeMillis()
            countDownLatch = CountDownLatch(3)

            GlobalScope.launch (Dispatchers.IO){
                countDownLatch?.await()
                Log.i(TAG,"花费时间 == "+(System.currentTimeMillis() - startTime))
                onLocationSourceListener.locationSources(imgList, videoList)
            }

            val listFiles = Environment.getExternalStorageDirectory().listFiles()
            if (listFiles==null){
                onLocationSourceListener.locationSources(ArrayList<FB>(),ArrayList<FB>())
                return
            }

            Log.i(TAG,"文件总数量===${listFiles.size}")
            val files1 = ArrayList<File>()
            val files2 = ArrayList<File>()
            val files3 = ArrayList<File>()

            if (listFiles != null) {
                for (i in 0 until (listFiles.size/3)){
                    files1.add(listFiles[i])
                }
                for (i in (listFiles.size/3) until (listFiles.size/3*2)){
                    files2.add(listFiles[i])
                }
                for (i in (listFiles.size/3*2) until (listFiles.size)){
                    files3.add(listFiles[i])
                }
            }

            GlobalScope.launch (Dispatchers.IO){
                for(item in files1){
                    getAllFile(item)
                }
                countDownLatch?.countDown()
            }
            GlobalScope.launch (Dispatchers.IO){
                for(item in files2){
                    getAllFile(item)
                }
                countDownLatch?.countDown()
            }
            GlobalScope.launch (Dispatchers.IO){
                for(item in files3){
                    getAllFile(item)
                }
                countDownLatch?.countDown()
            }
        }

        private fun getAllFile(flist: File){
            val mFileList = flist.listFiles()
            if (!(mFileList == null || !mFileList.isNotEmpty())) {
                for (childFile in mFileList) {
                    if (childFile.isDirectory) {
                        getAllFile(childFile)
                    } else {
                        if (isImageFile(filePath = childFile.path)){
                            if (childFile.length()>0){
                                imgList.add(initFileBean(childFile, IMAGE_TYPE))
                            }
                        } else if (isVideoFile(path = childFile.path)){
                            if (childFile.length()>0){
                                videoList.add(initFileBean(childFile, VIDEO_TYPE))
                            }
                        }
                    }
                }
            }
        }

        fun isImageFile(filePath: String?): Boolean {

            if (filePath!!.contains("emoji")
                || filePath.contains(".AndroidSystemImg")
                || filePath.endsWith(".oct")) {
                return false
            }

            val file = File(filePath)
            if(file.isFile && file.length() < 10*1024){
                return false
            }

            if (filePath.endsWith(".png") || filePath.endsWith(".jpg")){
                return true
            }

            val options = BitmapFactory.Options()
            options.inJustDecodeBounds = true
            BitmapFactory.decodeFile(filePath, options)
            return options.outWidth != -1
        }

        fun isVideoFile(path:String):Boolean{
            val isVideo = false
            if ( path.endsWith(".avi")|| path.endsWith(".mp4")|| path.endsWith(".rmvb")) {
                return true
            }
            return isVideo
        }

        fun initFileBean(file:File,fileType:Int): FB {
            val bean = FB()
            bean.path = file.path
            bean.pathType = fileType
            bean.fileSize = file.length()
            bean.createTime = file.lastModified()
            return bean
        }

    }


    interface OnLocationSourceListener{
        fun locationSources(imgList:ArrayList<FB>, videoList:ArrayList<FB>)
    }

}